package edu.scut.course_management.po;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/* 持久化对象：学生
* @author thompson
*/
public class Student implements UserDetails {
    
    private static final long serialVersionUID = 5665380637666309685L;
    private Integer roleId;
    @JsonIgnore
    List<Role> roles;  // 用户权限列表
    private String studentId;
    private String password;
    private String name;
    private boolean male;
    private String address;
    private String phoneNumber;
    private Integer majorId;
    private String enrollmentYear;
    private String className;

    @Override
    public String getUsername() {
        return getStudentId();
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
    
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return getRoles();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isMale() {
        return male;
    }

    public void setMale(boolean male) {
        this.male = male;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public Integer getMajorId() {
        return majorId;
    }

    public void setMajorId(Integer majorId) {
        this.majorId = majorId;
    }

    public String getEnrollmentYear() {
        return enrollmentYear;
    }

    public void setEnrollmentYear(String enrollmentYear) {
        this.enrollmentYear = enrollmentYear;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    @Override
    public String toString() {
        return "Student {" + 
               "address=" + address + 
               ", className=" + className + 
               ", enrollmentYear=" + enrollmentYear + 
               ", majorId=" + majorId + 
               ", male=" + male + 
               ", name=" + name + 
               ", password=" + password + 
               ", phoneNumber=" + phoneNumber + 
               ", roleId=" + roleId + 
               ", studentId=" + studentId + 
               "}";
    }

    @Override
    public boolean equals(Object obj){
        if(!(obj instanceof Student)){
            return false;
        }
        else{
            return roleId.equals(((Student)obj).getRoleId())&&
                   studentId.equals(((Student)obj).getStudentId())&&
                   password.equals(((Student)obj).getPassword())&&
                   name.equals(((Student)obj).getName())&&
                   (male==((Student)obj).isMale())&&
                   address.equals(((Student)obj).getAddress())&&
                   phoneNumber.equals(((Student)obj).getPhoneNumber())&&
                   majorId.equals(((Student)obj).getMajorId())&&
                   enrollmentYear.equals(((Student)obj).getEnrollmentYear())&&
                   className.equals(((Student)obj).getClassName());
        }
        //(male==((Student)obj).isMale())这里不知道会不会有问题
    }

    public Student(Integer roleId, String studentId, String password, String name, boolean male, String address,
            String phoneNumber, Integer majorId, String enrollmentYear, String className) {
        this.roleId = roleId;
        this.studentId = studentId;
        this.password = password;
        this.name = name;
        this.male = male;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.majorId = majorId;
        this.enrollmentYear = enrollmentYear;
        this.className = className;
        this.roles = new LinkedList<Role>();
    }

    public Student(String studentId, String password, String name, boolean male, String address,
            String phoneNumber, Integer majorId, String enrollmentYear, String className) {
        this.studentId = studentId;
        this.password = password;
        this.name = name;
        this.male = male;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.majorId = majorId;
        this.enrollmentYear = enrollmentYear;
        this.className = className;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(Role role) {
        this.roles.add(role);
    }  

    // 生成基本账号信息
    public Map<String, Object> generateBasicInfoMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", studentId);
        map.put("name", name);
        map.put("roleName",roles.get(0).getRoleName());
        return map;
    }
}